<template>
  <view class="info-page">
    <!-- 导航栏 -->
    <view class="nav-bar">
      <view class="nav-left" @click="handleBack">
        <image class="back-icon" src="/static/jiantou2_1.png" mode="aspectFit"></image>
      </view>
      <view class="nav-title">消息中心</view>
      <view class="nav-right" @click="yidu()">
        <text style="font-size: 28rpx; color: #4CAF50;">全部已读</text>
      </view>
    </view>
    

    
    <!-- 消息列表 -->
    <scroll-view 
      class="info-list" 
      scroll-y 
    >
      <!-- 消息列表项 -->
      <view 
        v-for="(item, index) in infoList" 
        :key="item.id" 
        class="info-item"
        @click="viewInfoDetail(item)"
      >
        <view class="info-icon">
          <image class="icon-image" :src="getInfoIcon(item.type)" mode="aspectFit"></image>
        </view>
        <view class="info-content">
          <view class="info-title">{{ item.title }}</view>
          <view class="info-desc">{{ item.content }}</view>
          <view class="info-meta">
            <view class="info-time">{{item.createtime}}</view>
<!--            <view class="info-type">{{ getInfoTypeText(item.type) }}</view> -->
          </view>
        </view>
        <view class="info-arrow">
          <image class="arrow-icon" src="/static/jiantou2_3.png" mode="aspectFit"></image>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view v-if="infoList.length === 0 && !loading" class="empty-state">
        <image class="empty-icon" src="/static/Icon.png" mode="aspectFit"></image>
        <text class="empty-text">暂无消息</text>
      </view>
      

    </scroll-view>
  </view>
</template>

<script>
	import { getmessageindex,getmessagereadAll,getmessagedetail } from '../../api';
	import store from '@/store'
export default {
  data() {
    return {
      infoList: [],
      loading: false
    }
  },
  onLoad() {

  },
  onShow() {
  	    this.init();
  },

  
  methods: {
    // 初始化数据
	async  yidu(){
		let res=await getmessagereadAll()
		
	},
  async  init() {
      this.loading = true;
      try {
        // 尝试从API获取数据
        let res = await getmessageindex();
        console.log(res,'res111');
        
        // 如果API返回了数据，使用API数据
        if ( res.code ==1 && res.data.data) {
          this.infoList = res.data.data;
        } else {
          // 否则使用简单的测试数据数组
          this.infoList = [
            {
              id: 1,
              title: '系统通知：欢迎使用台球馆管理系统',
              description: '尊敬的用户，欢迎使用我们的台球馆管理系统，如有任何问题请联系客服。',
              type: 'system',
              createTime: Date.now() - 24 * 60 * 60 * 1000,
              content: '这是系统通知的详细内容...'
            },
            {
              id: 2,
              title: '活动公告：周末台球比赛',
              description: '本周六将举办台球比赛，欢迎各位爱好者报名参加，赢取丰厚奖品！',
              type: 'activity',
              createTime: Date.now() - 2 * 24 * 60 * 60 * 1000,
              content: '这是活动公告的详细内容...'
            },
            {
              id: 3,
              title: '新闻资讯：台球新规则发布',
              description: '国际台球协会发布了最新的比赛规则，快来了解一下吧。',
              type: 'news',
              createTime: Date.now() - 3 * 24 * 60 * 60 * 1000,
              content: '这是新闻资讯的详细内容...'
            }
          ];
        }
      } catch (error) {
        console.error('获取消息列表失败:', error);
        // 错误时使用空数组
        this.infoList = [];
      } finally {
        this.loading = false;
      }
    },
    

    // 返回上一页
    handleBack() {
      uni.navigateBack();
    },
    
    // 查看信息详情
    viewInfoDetail(item) {
      // console.log('查看信息详情:', item);
      // 这里可以跳转到信息详情页
      uni.navigateTo({
        url: `/pages/xinxi/compontents/xinxiedit?id=${item.id}`
      });
    },
    

    
    // 格式化时间
    formatTime(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hour = String(date.getHours()).padStart(2, '0');
      const minute = String(date.getMinutes()).padStart(2, '0');
      
      return `${year}-${month}-${day} ${hour}:${minute}`;
    },
    
    // 获取信息类型文本
    getInfoTypeText(type) {
      const typeMap = {
        'notice': '通知',
        'news': '新闻',
        'activity': '活动',
        'system': '系统消息'
      };
      return typeMap[type] || '其他';
    },
    
    // 获取信息图标
    getInfoIcon(type) {
      const iconMap = {
        'notice': '/static/Group_427319261.png',
        'news': '/static/Group_427319272.png',
        'activity': '/static/Group_427319274.png',
        'system': '/static/Group_427319270.png'
      };
      return iconMap[type] || '/static/Group_427319269.png';
    },
    

  }
}
</script>

<style scoped>
/* 页面容器 */
.info-page {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  flex-direction: column;
}

/* 导航栏 */
.nav-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100rpx;
  background-color: #fff;
  padding: 0 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

.nav-left,
.nav-right {
  width: 60rpx;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.back-icon {
  width: 40rpx;
  height: 40rpx;
}

.nav-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
  flex: 1;
  text-align: center;
}

/* 搜索栏 */
.search-bar {
  padding: 20rpx 30rpx;
  background-color: #fff;
  margin-top: 100rpx;
}

.search-input-container {
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 60rpx;
  padding: 0 30rpx;
  height: 70rpx;
}

.search-icon {
  width: 30rpx;
  height: 30rpx;
  margin-right: 20rpx;
}

.search-input {
  flex: 1;
  height: 100%;
  font-size: 28rpx;
  color: #333;
}

/* 信息列表 */
.info-list {
  flex: 1;
  padding: 20rpx 30rpx;
  width: 750rpx;
  box-sizing: border-box;
}

/* 列表项 */
.info-item {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.info-item:active {
  transform: scale(0.98);
  box-shadow: 0 1rpx 5rpx rgba(0, 0, 0, 0.05);
}

.info-icon {
  width: 80rpx;
  height: 80rpx;
  border-radius: 16rpx;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 30rpx;
}

.icon-image {
  width: 50rpx;
  height: 50rpx;
}

.info-content {
  flex: 1;
  min-width: 0;
}

.info-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info-desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 15rpx;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.info-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-time {
  font-size: 24rpx;
  color: #999;
}

.info-type {
  font-size: 22rpx;
  color: #4CAF50;
  padding: 4rpx 16rpx;
  background-color: #E8F5E9;
  border-radius: 12rpx;
}

.info-arrow {
  margin-left: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.arrow-icon {
  width: 24rpx;
  height: 24rpx;
  color: #999;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  opacity: 0.5;
  margin-bottom: 30rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

/* 加载更多 */
.load-more {
  text-align: center;
  padding: 30rpx 0;
  color: #999;
  font-size: 26rpx;
}

.loading-text {
  color: #4CAF50;
}

.no-more-text {
  color: #ccc;
}

/* 响应式调整 */
@media (min-width: 768px) {
  .info-page {
    max-width: 768px;
    margin: 0 auto;
  }
}
</style>